﻿using System.Collections.Generic;
using Light.Framework.Castle;
using NHibernate.Criterion;
using Light.Framework.Model;
using Light.Framework.Contract;
using System.Security.Permissions;
using Light.Utility;

namespace Light.Framework.Component
{
    public class RoleComponent : BaseComponent<Role>, IRoleComponent
    {
        [AccessSecurity(SecurityAction.Demand)]
        public override OperateResult Delete(int id)
        {
            OperateResult result = new OperateResult();

            Role model = Get(id);
            if (model.IsSystem)
            {
                result.IsSucced = false;
                result.Message = "系统角色,不允许删除！";
            }
            else
            {
                result = base.Delete(id);
            }
            return result;
        }

        [AccessSecurity(SecurityAction.Demand)]
        public override OperateResult Update(Role obj)
        {
            OperateResult result = new OperateResult();

            if (obj.IsSuper && obj.IsPublic)
            {
                result.IsSucced = false;
                result.Message = "超级管理员不允许公用！";
            }
            if (!ServerApplication.User.IsSuper && obj.IsSystem)
            {
                result.IsSucced = false;
                result.Message = "系统角色不允许修改！";
                return result;
            }
            else if (!obj.IsSuper && ServerApplication.User.Roles.Contains(obj))
            {
                result.IsSucced = false;
                result.Message = "用户不能修改自己所属的角色，请联系管理员！";
                return result;
            }

            else
            {
                return base.Update(obj);
            }
        }

        protected override ICriterion CreateCriterion(Role obj)
        {
            return Expression.And(Expression.Eq("Name", obj.Name), Expression.Eq("Organization", obj.Organization));
        }

        protected override System.Func<Role, bool> CreateFunc(Role model)
        {
            return r => r.Name == model.Name && r.Organization.Equals(model.Organization) && !r.IsDeleted;
        }

        public override IList<Role> GetAll()
        {

            DetachedCriteria criteria = DetachedCriteria.For<Role>();

            ICriterion criterion = base.GetNotDeletedCriterion();

            if (!ServerApplication.User.IsSuper)
            {
                ICriterion criterion1 = Expression.Or(Expression.Eq("IsPublic", true), GetCriterion(null));
                criterion = Expression.And(criterion, criterion1);
            }
            criteria.Add(criterion);

            return base.GetAll(criteria);
        }


        public Role GetGuest()
        {
            return EntityMediator<Role>.FindOneOrDefault(Expression.Eq("Name", "guest"));
        }

        protected override IList<Role> Search(DetachedCriteria criterion, int pageIndex, int pageSize, out int totalResult)
        {


            if (ModelOrder.IsEmpty)
                ModelOrder.Add("DisplayOrder", true);

            ICriterion criterion1 = base.GetNotDeletedCriterion();
            if (!ServerApplication.User.IsSuper)
            {
                criterion1 = Expression.And(criterion1, Expression.Eq("IsSuper", false));
                criterion1 = Expression.And(criterion1, Expression.Eq("IsSystem", false));
            }

            criterion.Add(criterion1);
            return base.Search(criterion, pageIndex, pageSize, out totalResult);
        }
    }

}
